package com.zrl.ggkt.vod.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrl.ggkt.model.vod.Teacher;
import com.zrl.ggkt.result.R;
import com.zrl.ggkt.vo.vod.TeacherQueryVo;
import com.zrl.ggkt.vod.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author zrl
 * @since 2022-08-09
 */
@Api(tags = "讲师管理接口 ")
@RestController
@RequestMapping("/admin/vod/teacher")
//@CrossOrigin
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @ApiOperation("查询所有讲师")
    @GetMapping("findAll")
    public R findAllTeacher(){
//        try {
//            int i = 10/0;
//        } catch (Exception e) {
//            throw new MyException(201,"执行自定义异常");
//        }
        List<Teacher> list = teacherService.list();
        return R.ok(list);
    }

    //根据id查询讲师
    @ApiOperation("根据id查询讲师")
    @GetMapping("findById/{id}")
    public R findTeacherById(@PathVariable Long id){
        Teacher teacher = teacherService.getById(id);
        return R.ok(teacher);
    }

    @ApiOperation("逻辑删除讲师")
    @DeleteMapping("remove/{id}")
    public R deleteTeacher(@ApiParam(name = "id", value = "ID", required = true) @PathVariable int id){
        boolean isSuccess = teacherService.removeById(id);
        if (isSuccess==true){
            return R.ok();
        }else{
            return R.fail();
        }
    }

    //条件查询分页
    @ApiOperation("条件查询分页")
    @PostMapping("findByPage/{current}/{limit}")
    public R findByPage(
            @ApiParam(name = "page",value = "当前页码",required = true)
            @PathVariable long current,
            @ApiParam(name = "limit",value = "每页记录数",required = true)
            @PathVariable long limit,
            @ApiParam(name = "teacherVo",value = "查询对象",required = false)
            @RequestBody(required = false) TeacherQueryVo teacherQueryVo){

        Page<Teacher> page = new Page<>(current,limit);

        if (teacherQueryVo==null){
            Page<Teacher> pageInfo = teacherService.page(page, null);
            return R.ok(pageInfo);
        }else{
            String name = teacherQueryVo.getName();
            Integer level = teacherQueryVo.getLevel();
            String joinDateBegin = teacherQueryVo.getJoinDateBegin();
            String joinDateEnd = teacherQueryVo.getJoinDateEnd();

            LambdaQueryWrapper<Teacher> queryWrapper = new LambdaQueryWrapper();
            if (!StringUtils.isEmpty(name)){
                queryWrapper.like(Teacher::getName,name);
            }
            if (!StringUtils.isEmpty(level)){
                queryWrapper.eq(Teacher::getLevel,level);
            }
            if (!StringUtils.isEmpty(joinDateBegin)){
                queryWrapper.ge(Teacher::getCreateTime,joinDateBegin);
            }
            if (!StringUtils.isEmpty(joinDateEnd)){
                queryWrapper.le(Teacher::getCreateTime,joinDateEnd);
            }
            //调用方法分页查询
            IPage<Teacher> pageInfo = teacherService.page(page, queryWrapper);
            return R.ok(pageInfo);
        }
    }
    //添加讲师
    @ApiOperation("添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody Teacher teacher){
        boolean isSuccess = teacherService.save(teacher);
        if (isSuccess==true){
            return R.ok();
        }else{
            return R.fail();
        }
    }

    @ApiOperation("修改讲师")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody Teacher teacher){
        boolean isSuccess = teacherService.updateById(teacher);
        if (isSuccess==true){
            return R.ok();
        }else{
            return R.fail();
        }
    }

    //批量删除讲师
    //json数组[1,2,3]
    @ApiOperation("批量删除讲师")
    @DeleteMapping("removeBatch")
    public R remove(@RequestBody List<Long> idList){
        boolean isSuccess = teacherService.removeByIds(idList);
        if (isSuccess==true){
            return R.ok();
        }else{
            return R.fail();
        }
    }


    //根据id查询讲师信息远程调用
    @ApiOperation("根据id查询讲师")
    @GetMapping("/inner/getTeacher/{id}")
    public Teacher findTeacherInfoById(@PathVariable Long id){
        Teacher teacher = teacherService.getById(id);
        return teacher;
    }

}

